<template>
  <el-col>
    <div v-if="dataList.length > 0">
      <div class="post-list" v-for="item in dataList" :key="item.id">
        <!-- 三张图片的列表 -->
        <div class="post-item" v-if="item.images.length >= 3">
          <h4 class="post-title">
            <nuxt-link :to="`/post/PostDetail?id=${item.id}`">
              {{ item.title }}
            </nuxt-link>
          </h4>
          <p class="post-desc">
            <a
              :href="`/post/PostDetail?id=${item.id}`"
              class=""
              v-html="item.summary"
            ></a>
          </p>
          <el-row>
            <el-col
              :span="8"
              class="img"
              v-for="(imgSrc, index) in item.images.filter((v, i) => i < 3)"
              :key="index"
            >
              <a :href="`/post/PostDetail?id=${item.id}`">
                <el-image :src="imgSrc" />
              </a>
            </el-col>
          </el-row>
          <div class="post-info">
            <div class="post-info-left">
              <span>
                <i class="el-icon-location-outline"></i>
                {{ item.cityName }}
              </span>
              <div class="post-user">
                by
                <a href="#">
                  <img
                    :src="
                      'http://157.122.54.189:9095' + item.account.defaultAvatar
                    "
                  />
                </a>
                <a href="#">{{ item.account.nickname }}</a>
              </div>
              <span>
                <i class="el-icon-view"></i>
                {{ item.watch }}
              </span>
            </div>
            <div class="post-info-right">
              <span>{{ item.like }} 赞</span>
            </div>
          </div>
        </div>
        <!-- 一张图片的列表 -->
        <div class="post-item2" v-if="item.images.length < 3">
          <div class="post-cover" v-if="item.images && item.images.length > 0">
            <a :href="`/post/PostDetail?id=${item.id}`">
              <el-image :src="item.images[0]" />
            </a>
          </div>

          <div class="post-content">
            <h4 class="post-title">
              <a :href="`/post/PostDetail?id=${item.id}`" class="">
                {{ item.title }}
              </a>
            </h4>
            <p class="post-desc">
              <a
                :href="`/post/PostDetail?id=${item.id}`"
                class=""
                v-html="item.summary"
              ></a>
            </p>
            <div class="post-info">
              <div class="post-info-left">
                <span>
                  <i class="el-icon-location-outline"></i>
                  {{ item.cityName }}
                </span>
                <div class="post-user">
                  by
                  <a href="#">
                    <img
                      :src="
                        'http://157.122.54.189:9095' +
                        item.account.defaultAvatar
                      "
                    />
                  </a>
                  <a href="#">{{ item.account.nickname }}</a>
                </div>
                <span>
                  <i class="el-icon-view"></i>
                  {{ item.watch }}
                </span>
              </div>
              <div class="post-info-right">
                <span>{{ item.like }} 赞</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else>暂时没有数据哦</div>
    <!-- 分页组件 -->
    <el-pagination
      layout="total, sizes, prev, pager, next ,jumper"
      :page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      :total="totalPage"
      @current-change="currentChange"
      @size-change="sizeChange"
    >
    </el-pagination>
  </el-col>
</template>

<script>
import bus from "./utils/postBus";
import { postList } from "@/utils/apis/post";
export default {
  data() {
    return {
      ArticlesList: [],
      //自己算出来的文章列表数据
      dataList: [],
      //分页组件，当前页码
      pageIndex: 1,
      //每页长度
      pageSize: 3,
      //文章列表总数据
      totalPage: 0,

      newpageIndex: 1,
      newpageSize: 3,

      searchList: [],
    };
  },
  async mounted() {
    let res = await postList();
    if (res.data.data) {
      this.ArticlesList = res.data.data;
      this.dataList = this.getDataList();
      this.totalPage = res.data.total;
    }

    bus.$on("postList", (res) => {
      // //分页组件数据切割数据的封装函数
      const begin = (this.pageIndex - 1) * this.pageSize;
      const end = begin + this.pageSize;
      this.dataList = res.data.data.slice(begin, end);

      this.totalPage = res.data.total;
      this.searchList = res.data.data;
    });
  },

  computed: {
    articleDetailPage() {
      return "/post/detail?id=" + this.item.id;
    },
  },
  methods: {
    currentChange(newIndex) {
      this.pageIndex = newIndex;
      if (this.$route.query.city) {
        this.dataList = this.getSearchList();
      } else {
        this.dataList = this.getDataList();
      }
    },
    sizeChange(newSize) {
      this.pageSize = newSize;
      if (this.$route.query.city) {
        this.dataList = this.getSearchList();
      } else {
        this.dataList = this.getDataList();
      }
    },
    getDataList() {
      //分页组件数据切割数据的封装函数
      const begin = (this.pageIndex - 1) * this.pageSize;
      const end = begin + this.pageSize;
      return this.ArticlesList.slice(begin, end);
    },
    getSearchList() {
      const begin = (this.pageIndex - 1) * this.pageSize;
      const end = begin + this.pageSize;
      return this.searchList.slice(begin, end);
    },
  },
};
</script>

<style lang="less" scoped>
.post-item {
  width: 100%;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
  h4 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 15px;
    font-weight: 400;
    font-size: 18px;
  }
  p {
    margin-bottom: 15px;
    line-height: 1.5;
    font-size: 14px;
    height: 63px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    a {
      color: #666;
    }
  }
  .img {
    a {
      text-decoration: none;
      color: inherit;
      /deep/ img {
        width: 220px;
        height: 150px;
        display: block;
        -o-object-fit: cover;
        object-fit: cover;
      }
    }
  }
  .post-info {
    display: flex;
    justify-content: space-between;
    .post-info-left {
      margin-top: 20px;
      display: flex;
      font-size: 12px;
      color: #999;
      span {
        margin-right: 10px;
      }
      .post-user {
        margin-right: 10px;
        display: flex;

        a {
          color: orange;
          img {
            display: block;
            width: 16px;
            height: 16px;
            border-radius: 100px;
            margin: 0 5px 5px 5px;
          }
        }
      }
    }
    .post-info-right {
      margin-top: 20px;
      span {
        color: orange;
      }
    }
  }
}
.post-item2 {
  display: flex;
  width: 100%;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
  .post-cover {
    width: 220px;
    height: 150px;
    overflow: hidden;
    flex-shrink: 0;
    margin-right: 10px;
    img {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .post-content {
    flex: 1;
    h4 {
      width: 470px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-bottom: 15px;
      font-weight: 400;
      font-size: 18px;
    }
    p {
      margin-bottom: 15px;
      line-height: 1.5;
      font-size: 14px;
      height: 63px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
    .post-info {
      display: flex;
      justify-content: space-between;
      .post-info-left {
        margin-top: 10px;
        display: flex;
        font-size: 12px;
        color: #999;
        span {
          margin-right: 10px;
        }
        .post-user {
          margin-right: 10px;
          display: flex;

          a {
            color: orange;
            img {
              display: block;
              width: 16px;
              height: 16px;
              border-radius: 100px;
              margin: 0 5px 5px 5px;
            }
          }
        }
      }
      .post-info-right {
        margin-top: 10px;
        span {
          color: orange;
        }
      }
    }
  }
}
</style>
